package com.zhuss.core.processor;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.user.UpdateUserBo;
import com.zhuss.bean.bo.core.user.UserDeptBindBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.UserDao;
import com.zhuss.core.db.entity.User;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.exception.WpsException;
import com.zhuss.util.JsonUtil;

/**
 * 简化
 * 创建用户 processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("userUpdateSimple")
public class UserUpdateSimple extends AbstractProcessor<Object, UpdateUserBo> implements Processor<Object, UpdateUserBo> {

    @Autowired
    private UserDao userDao;
    
    @Autowired
	@Qualifier("userDeptBind")
	private Processor<Object, UserDeptBindBo> userDeptBind;
    
    
    /**
     * 校验
     */
    @Override
    public void check(UpdateUserBo bo) throws WpsException {
    }
    
    /**
     * 操作方法
     * @throws WpsException 
     */
    @Override
    public Object process(UpdateUserBo param) throws WpsException {
    	User record = new User();
    	record.setUserId(param.getUserId());
    	record.setAge(param.getAge());
    	record.setArea(param.getArea());
    	record.setCompanyId(param.getCompanyId());
    	record.setIsLimitArea(param.getIsLimitArea());
    	record.setName(param.getName());
    	record.setPhone(param.getPhone());
    	record.setSex(param.getSex());
    	record.setUsername(param.getUsername());
    	record.setAddress(param.getAddress());
    	record.setProfession(param.getProfession());
    	record.setWx(param.getWx());
    	record.setBlog(param.getBlog());
    	record.setQq(param.getQq());
    	
    	record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
    	try {
    		userDao.updateByPrimaryKeySelective(record);
    	} catch (Exception e) {
    		throw new WpsException(ExceptionCode.名称已被人使用);
    	}
    	
    	List<Long> deptIdList = param.getDeptIdList();
    	if (deptIdList == null) {
    		deptIdList = new ArrayList<Long>();
    	}
    	UserDeptBindBo bo = new UserDeptBindBo();
    	Long userId = record.getUserId();
    	bo.setUserId(userId);
    	bo.setDeptIdList(deptIdList);
    	userDeptBind.process(bo);
    	
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(UpdateUserBo param) throws WpsException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.用户修改.key());
    	record.setTargetId(param.getUserId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
